﻿// =================================================================== 
// 数据访问层(GDIT.DAL)
//====================================================================
// 文件：Mtb_Users.cs
// 项目名称：学院网站
// 创建时间：2012-08-08
// 负责人：ChenZhiGang
//引用DevExpress.Data.Filtering，DevExpress.Xpo程序集
// 程序集介绍：用户信息数据访问层
// ===================================================================
using GDIT.Model;
using DevExpress.Data.Filtering;
using DevExpress.Xpo;
using GDIT.Pattern;

namespace GDIT.DAL
{
    public class DUsers : IUsers
    {

        XpoHelper.Test<Mtb_Users> userOperation = null;

        /// <summary>
        /// 判断登陆是否通过
        /// </summary>
        /// <param name="name">用户名</param>
        /// <param name="password">用户密码</param>
        /// <returns></returns>
        public bool IsPass(string name, string password)
        {
            bool result = false;
            GroupOperator criteria = new GroupOperator();
            criteria.Operands.Add(new BinaryOperator("UserName", name, BinaryOperatorType.Equal));
            criteria.Operands.Add(new BinaryOperator("UserPwd", password, BinaryOperatorType.Equal));
            userOperation = new XpoHelper.Test<Mtb_Users>();
            XPCollection xp = userOperation.Operation(criteria);
            if (xp.Count == 1)//登陆通过
            {
                result = true;
            }
            else
            {
                result = false;
            }
            return result;
        }

        /// <summary>
        /// 根据用户名查询用户角色
        /// </summary>
        /// <param name="name">用户名</param>
        /// <returns>用户所拥有的系统角色，没有则返回空</returns>
        public string SelectRole(string name)
        {
            string role = string.Empty;
            userOperation = new XpoHelper.Test<Mtb_Users>();
            XPCollection xp = userOperation.Operation("UserName", name);
            foreach (Mtb_Users u in xp)
            {
                role = u.Role.RoleName;
            }
            return role;
        }

        /// <summary>
        /// 根据用户名查询用户所在部门
        /// </summary>
        /// <param name="name">用户名</param>
        /// <returns>返回用户所在部门，没有则返回空</returns>
        public string SelectDepartment(string name)
        {
            string department = string.Empty;
            userOperation = new XpoHelper.Test<Mtb_Users>();
            XPCollection xp = userOperation.Operation("UserName", name);
            foreach (Mtb_Users u in xp)
            {
                department = u.Department.DepartmentName;
            }
            return department;
        }

        /// <summary>
        /// 根据用户名修改用户角色
        /// </summary>
        /// <param name="name">用户名</param>
        /// <param name="role">将要修改的用户角色</param>
        public int UpdateRole(string name, string role)
        {
            int result = 0;
            userOperation = new XpoHelper.Test<Mtb_Users>();
            XPCollection xp = userOperation.Operation("UserName", name);
            foreach (Mtb_Users u in xp)
            {
                u.Role.RoleName = role;
                result = XpoHelper.SaveModified(u);
            }
            return result;
        }

        /// <summary>
        /// 根据用户名修改用户所在部门
        /// </summary>
        /// <param name="name">用户名</param>
        /// <param name="department">用户所在部门</param>
        public int UpdatePosition(string name, string department)
        {
            int result = 0;
            userOperation = new XpoHelper.Test<Mtb_Users>();
            XPCollection xp = userOperation.Operation("UserName", name);
            foreach (Mtb_Users u in xp)
            {
                u.Department.DepartmentName = department;
                result = XpoHelper.SaveModified(u);
            }
            return result;
        }

        /// <summary>
        /// 根据用户名修改用户密码
        /// </summary>
        /// <param name="name">用户名</param>
        /// <param name="password">用户将要修改的用户密码</param>
        public int UpdatePassword(string name, string password)
        {
            int result = 0;
            userOperation = new XpoHelper.Test<Mtb_Users>();
            XPCollection xp = userOperation.Operation("UserName", name);
            foreach (Mtb_Users u in xp)
            {
                u.UserPwd = password;
                result = XpoHelper.SaveModified(u);
            }
            return result;
        }

        /// <summary>
        /// 修改用户个人资料
        /// </summary>
        /// <param name="user"></param>
        public int UpdateUserData(Mtb_Users user)
        {
            int result = 0;
            userOperation = new XpoHelper.Test<Mtb_Users>();
            XPCollection xp = userOperation.Operation("UserName", user.UserName);
            foreach (Mtb_Users u in xp)
            {
                u.UserName = user.UserName;
                u.UserPwd = user.UserPwd;
                result = XpoHelper.SaveModified(u);
            }
            return result;
        }

        /// <summary>
        /// 判断用户名是否已经存在
        /// </summary>
        /// <param name="name">将要添加的用户名</param>
        /// <returns>true：用户名存在  false：用户名不存在</returns>
        public bool IsExist(string name)
        {
            bool result;
            userOperation = new XpoHelper.Test<Mtb_Users>();
            XPCollection xp = userOperation.Operation("UserName", name);
            if (xp.Count == 1)//用户名存在
            {
                result = true;
            }
            else
            {
                result = false;
            }
            return result;
        }

        /// <summary>
        /// 根据用户名返回一个用户对象
        /// </summary>
        /// <param name="name">用户名</param>
        /// <returns></returns>
        public Mtb_Users GetUser(string name)
        {
            Mtb_Users user = null;
            userOperation = new XpoHelper.Test<Mtb_Users>();
            XPCollection xp = userOperation.Operation("UserName", name);
            foreach (Mtb_Users u in xp)
            {
                user = u;
            }
            return user;
        }

        /// <summary>
        /// 返回所有用户资料
        /// </summary>
        /// <returns>返回所有用户的资料</returns>
        public XPCollection GetAllUser()
        {
            userOperation = new XpoHelper.Test<Mtb_Users>();
            XPCollection xp = userOperation.Operation();
            //按照Id进行降序排列
            SortProperty sp = new SortProperty("OID", DevExpress.Xpo.DB.SortingDirection.Descending);
            xp.Sorting.Add(sp);
            return xp;
        }

        /// <summary>
        /// 新增一个用户
        /// </summary>
        /// <param name="user">需要新增的用户对象</param>
        /// <returns></returns>
        public int AddUser(Mtb_Users user)
        {
            return XpoHelper.SaveAdded(user);
        }

        /// <summary>
        /// 增加公告文章
        /// </summary>
        /// <param name="user"></param>
        /// <returns></returns>
        public int AddUserBulletinArticle(Mtb_Users user)
        {
            int result = 0;
            XPCollection xp = new XPCollection(typeof(Mtb_Users),new BinaryOperator("UserName",user.UserName));
            foreach (Mtb_Users u in xp)
            {
                u.BulletinArticles.AddRange(user.BulletinArticles);
                result = XpoHelper.SaveModifiedByZi(u);
            }
            return result;
        }

        /// <summary>
        /// 增加校园资讯文章
        /// </summary>
        /// <param name="user"></param>
        /// <returns></returns>
        public int AddUserCollegeArticle(Mtb_Users user)
        {
            int result = 0;
            XPCollection xp = new XPCollection(typeof(Mtb_Users), new BinaryOperator("UserName", user.UserName));
            foreach (Mtb_Users u in xp)
            {
                u.CollegeInformationArticles.AddRange(user.CollegeInformationArticles);
                result = XpoHelper.SaveModifiedByZi(u);
            }
            return result;
        }

        /// <summary>
        /// 删除一个用户
        /// </summary>
        /// <param name="user">需要删除的用户对象</param>
        /// <returns></returns>
        public int DeleteUser(int id)
        {
            userOperation = new XpoHelper.Test<Mtb_Users>();
            return userOperation.Delete(id);
        }
    }
}
